# Author: Patrick Cunha Silva
# Project: Janusz, Cunha, and Junqueira
# Goal: Run RD models for Blacks + Women (SI Analysis)
# First Version: Dec 14, 2023
# This Version: Jun 4, 2024

rm(list = ls())

# Load data
load(file = here("Data", "blackRDD_final.RData"))

##############################################################
##############################################################
##################### Black Women ############################
##############################################################
##############################################################

# Find blacks + women winners
blackRDD$blackWomenWinner <- as.numeric(blackRDD$DS_COR_RACA_WINNER %in% c('PARDA', 'PRETA') & blackRDD$DS_GENERO_WINNER == 'FEMININO')
# Find blacks + women runnerup
blackRDD$blackWomenRunnerup <- as.numeric(blackRDD$DS_COR_RACA_RUNNERUP %in% c('PARDA', 'PRETA') & blackRDD$DS_GENERO_RUNNERUP == 'FEMININO')
# Keep only elections that have a black woman and a men as the runner-up
blackWomenRDD <- subset(blackRDD, (blackWomenWinner == 1 & DS_GENERO_RUNNERUP != 'FEMININO') | (blackWomenRunnerup == 1 & DS_GENERO_WINNER != 'FEMININO'))

##############################################################
########### Part 1 - RD for Ratio, No Restrictions ###########
##############################################################

# N of Candidates
m1a <- rdrobust(y = blackWomenRDD$totalBlackWomen,
                x = blackWomenRDD$run_var_black,
                all = TRUE)
summary(m1a)

# Ratio
m1b <- rdrobust(y = blackWomenRDD$totalBlackWomen_ratio,
                x = blackWomenRDD$run_var_black,
                all = TRUE)
summary(m1b) 

# Percent
m1c <- rdrobust(y = blackWomenRDD$totalBlackWomen_pct,
                x = blackWomenRDD$run_var_black,
                all = TRUE)
summary(m1c) 

#############################################
########### Part 2 - No Experience ##########
#############################################

# N of Candidates
m2a  <- rdrobust(y = blackWomenRDD$totalBlackWomen_noexp,
                 x = blackWomenRDD$run_var_black,
                 all = TRUE)
summary(m2a) 

# Ratio
m2b  <- rdrobust(y = blackWomenRDD$totalBlackWomen_noexp_ratio,
                 x = blackWomenRDD$run_var_black,
                 all = TRUE)
summary(m2b)

# Percent
m2c  <- rdrobust(y = blackWomenRDD$totalBlackWomen_noexp_pct,
                 x = blackWomenRDD$run_var_black,
                 all = TRUE)
summary(m2c) 


##########################################################
########### Part 3 - No Experience From Mayoral ##########
##########################################################

# N of Candidates
m3a  <- rdrobust(y = blackWomenRDD$totalBlackWomen_noexp_frommp,
                 x = blackWomenRDD$run_var_black,
                 all = TRUE)
summary(m3a)

# Ratio
m3b  <- rdrobust(y = blackWomenRDD$totalBlackWomen_noexp_frommp_ratio,
                 x = blackWomenRDD$run_var_black,
                 all = TRUE)
summary(m3b) 

# Percent
m3c  <- rdrobust(y = blackWomenRDD$totalBlackWomen_noexp_frommp_pct,
                 x = blackWomenRDD$run_var_black,
                 all = TRUE)
summary(m3c)

##############################################################
##############################################################
##################### Black Men ##############################
##############################################################
##############################################################

# Find blacks + men winners
blackRDD$blackMenWinner <- as.numeric(blackRDD$DS_COR_RACA_WINNER %in% c('PARDA', 'PRETA') & blackRDD$DS_GENERO_WINNER == 'MASCULINO')
# Find blacks + men runnerup
blackRDD$blackMenRunnerup <- as.numeric(blackRDD$DS_COR_RACA_RUNNERUP %in% c('PARDA', 'PRETA') & blackRDD$DS_GENERO_RUNNERUP == 'MASCULINO')
# Keep only elections that have a black woman
blackMenRDD <- subset(blackRDD, (blackMenWinner == 1 & DS_GENERO_RUNNERUP != 'MASCULINO') | (blackMenRunnerup == 1 & DS_GENERO_WINNER != 'MASCULINO'))


##############################################################
########### Part 1 - RD for Ratio, No Restrictions ###########
##############################################################

# N of Candidates
m1a <- rdrobust(y = blackMenRDD$totalBlackMen,
                x = blackMenRDD$run_var_black,
                all = TRUE)
summary(m1a) 

# Ratio
m1b <- rdrobust(y = blackMenRDD$totalBlackMen_ratio,
                x = blackMenRDD$run_var_black,
                all = TRUE)
summary(m1b) 

# Percent
m1c <- rdrobust(y = blackMenRDD$totalBlackMen_pct,
                x = blackMenRDD$run_var_black,
                all = TRUE)
summary(m1c) 

#############################################
########### Part 2 - No Experience ##########
#############################################

# N of Candidates
m2a  <- rdrobust(y = blackMenRDD$totalBlackMen_noexp,
                 x = blackMenRDD$run_var_black,
                 all = TRUE)
summary(m2a) 

# Ratio
m2b  <- rdrobust(y = blackMenRDD$totalBlackMen_noexp_ratio,
                 x = blackMenRDD$run_var_black,
                 all = TRUE)
summary(m2b) 

# Percent
m2c  <- rdrobust(y = blackMenRDD$totalBlackMen_noexp_pct,
                 x = blackMenRDD$run_var_black,
                 all = TRUE)
summary(m2c) 

##########################################################
########### Part 3 - No Experience From Mayoral ##########
##########################################################

# N of Candidates
m3a  <- rdrobust(y = blackMenRDD$totalBlackMen_noexp_frommp,
                 x = blackMenRDD$run_var_black,
                 all = TRUE)
summary(m3a) 

# Ratio
m3b  <- rdrobust(y = blackMenRDD$totalBlackMen_noexp_frommp_ratio,
                 x = blackMenRDD$run_var_black,
                 all = TRUE)
summary(m3b) 

# Percent
m3c  <- rdrobust(y = blackMenRDD$totalBlackMen_noexp_frommp_pct,
                 x = blackMenRDD$run_var_black,
                 all = TRUE)
summary(m3c) 


##############################################################
##############################################################
##################### White Women ############################
##############################################################
##############################################################

# Find white + women winners
blackRDD$WhiteWomenWinner <- as.numeric(blackRDD$DS_COR_RACA_WINNER %in% c('BRANCA') & blackRDD$DS_GENERO_WINNER == 'FEMININO')
# Find white + women runnerup
blackRDD$WhiteWomenRunnerup <- as.numeric(blackRDD$DS_COR_RACA_RUNNERUP %in% c('BRANCA') & blackRDD$DS_GENERO_RUNNERUP == 'FEMININO')
# Keep only elections that have a black woman
whiteWomenRDD <- subset(blackRDD, (WhiteWomenWinner == 1 & DS_GENERO_RUNNERUP != 'FEMININO') | (WhiteWomenRunnerup == 1 & DS_GENERO_WINNER != 'FEMININO'))


##############################################################
########### Part 1 - RD for Ratio, No Restrictions ###########
##############################################################

# N of Candidates
m1a <- rdrobust(y = whiteWomenRDD$totalWhiteWomen,
                x = whiteWomenRDD$run_var_white,
                all = TRUE)
summary(m1a)

# Ratio
m1b <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_ratio,
                x = whiteWomenRDD$run_var_white,
                all = TRUE)
summary(m1b) 

# Percent
m1c <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_pct,
                x = whiteWomenRDD$run_var_white,
                all = TRUE)
summary(m1c) 

#############################################
########### Part 2 - No Experience ##########
#############################################

# N of Candidates
m2a  <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_noexp,
                 x = whiteWomenRDD$run_var_white,
                 all = TRUE)
summary(m2a) 

# Ratio
m2b  <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_noexp_ratio,
                 x = whiteWomenRDD$run_var_white,
                 all = TRUE)
summary(m2b)

# Percent
m2c  <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_noexp_pct,
                 x = whiteWomenRDD$run_var_white,
                 all = TRUE)
summary(m2c) 

##########################################################
########### Part 3 - No Experience From Mayoral ##########
##########################################################

# N of Candidates
m3a  <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_noexp_frommp,
                 x = whiteWomenRDD$run_var_white,
                 all = TRUE)
summary(m3a) 

# Ratio
m3b  <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_noexp_frommp_ratio,
                 x = whiteWomenRDD$run_var_white,
                 all = TRUE)
summary(m3b) 

# Percent
m3c  <- rdrobust(y = whiteWomenRDD$totalWhiteWomen_noexp_frommp_pct,
                 x = whiteWomenRDD$run_var_white,
                 all = TRUE)
summary(m3c) 



##############################################################
##############################################################
##################### White Men ##############################
##############################################################
##############################################################

# Find white + men winners
blackRDD$whiteMenWinner <- as.numeric(blackRDD$DS_COR_RACA_WINNER %in% c('BRANCA') & blackRDD$DS_GENERO_WINNER == 'MASCULINO')
# Find white + men runnerup
blackRDD$whiteMenRunnerup <- as.numeric(blackRDD$DS_COR_RACA_RUNNERUP %in% c('BRANCA') & blackRDD$DS_GENERO_RUNNERUP == 'MASCULINO')
# Keep only elections that have a white man
whiteMenRDD <- subset(blackRDD, (whiteMenWinner == 1 & DS_GENERO_RUNNERUP != 'MASCULINO') | (whiteMenRunnerup == 1 & DS_GENERO_WINNER != 'MASCULINO'))

##############################################################
########### Part 1 - RD for Ratio, No Restrictions ###########
##############################################################

# N of Candidates
m1a <- rdrobust(y = whiteMenRDD$totalWhiteMen,
                x = whiteMenRDD$run_var_white,
                all = TRUE)
summary(m1a)

# Ratio
m1b <- rdrobust(y = whiteMenRDD$totalWhiteMen_ratio,
                x = whiteMenRDD$run_var_white,
                all = TRUE)
summary(m1b)

# Percent
m1c <- rdrobust(y = whiteMenRDD$totalWhiteMen_pct,
                x = whiteMenRDD$run_var_white,
                all = TRUE)
summary(m1c) 

#############################################
########### Part 2 - No Experience ##########
#############################################

# N of Candidates
m2a  <- rdrobust(y = whiteMenRDD$totalWhiteMen_noexp,
                 x = whiteMenRDD$run_var_white,
                 all = TRUE)
summary(m2a) 

# Ratio
m2b  <- rdrobust(y = whiteMenRDD$totalWhiteMen_noexp_ratio,
                 x = whiteMenRDD$run_var_white,
                 all = TRUE)
summary(m2b) 

# Percent
m2c  <- rdrobust(y = whiteMenRDD$totalWhiteMen_noexp_pct,
                 x = whiteMenRDD$run_var_white,
                 all = TRUE)
summary(m2c) 


##########################################################
########### Part 3 - No Experience From Mayoral ##########
##########################################################

# N of Candidates
m3a  <- rdrobust(y = whiteMenRDD$totalWhiteMen_noexp_frommp,
                 x = whiteMenRDD$run_var_white,
                 all = TRUE)
summary(m3a) 

# Ratio
m3b  <- rdrobust(y = whiteMenRDD$totalWhiteMen_noexp_frommp_ratio,
                 x = whiteMenRDD$run_var_white,
                 all = TRUE)
summary(m3b) 

# Percent
m3c  <- rdrobust(y = whiteMenRDD$totalWhiteMen_noexp_frommp_pct,
                 x = whiteMenRDD$run_var_white,
                 all = TRUE)
summary(m3c) 

